﻿namespace Grep.SharePoint.SmartDocuments.Code
{
    using Grep.Lib.Web.UI.Extensions;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using Menu=Microsoft.SharePoint.WebControls.Menu;
    using WebPart=System.Web.UI.WebControls.WebParts.WebPart;

    /// <summary>
    /// A class which fixes various bugs in ListViewWebPart instances
    /// </summary>
    internal sealed class ListViewBugFixes
    {
        /// <summary>
        /// Fixes an issue where the NewMenu will close itself on mouse hovering
        /// on submenu items.
        /// </summary>
        public static void FixSplitMenuJavaScriptBug(WebPart wp)
        {
            var listView = wp as ListViewWebPart;
            var newMenu = listView.FindChildByType<NewMenu>();
            Menu menu = newMenu.MenuControl;
            if (menu.MenuFormat == MenuFormat.ArrowSplitButton)
            {
                wp.Page.ClientScript.RegisterStartupScript(typeof (ListViewBugFixes), "ListViewBugFixes",
                                                           @"document.getElementById('" + menu.ClientID +
                                                           @"_t').onmouseover = function(e) { try { if(g_menuHtc_lastMenu && g_menuHtc_lastMenu._arrSelected && g_menuHtc_lastMenu._arrSelected[0] != null) return false; } catch(ex) {}; MMU_PopMenuIfShowing(this.rows[0].cells[1]); MMU_EcbTableMouseOverOut(this, true); };",
                                                           true);
            }
        }
    }
}